package game;

import java.util.Random;

import jgame.*;
import jgame.platform.*;

public class Streets extends JGEngine {

	static int numOfStreets = 6;
	static int spaceBetweenStreets = 4;
	static int roxelLength = numOfStreets*spaceBetweenStreets+numOfStreets+spaceBetweenStreets;
	static int roxelWidth = 16;
	private static Random rnd = new Random();
	
	
	public static void main(String [] args) {
		new Streets(new JGPoint(roxelLength*roxelWidth,roxelLength*roxelWidth));
	}

	
	
	/** Application constructor. */
	public Streets(JGPoint size) { initEngine(size.x,size.y); }

	/** Applet constructor. */
	public Streets() { initEngineApplet(); }

	public void initCanvas() { setCanvasSettings(roxelLength,roxelLength,roxelWidth,roxelWidth,null,null,null); }

	public void initGame() {
		setFrameRate(35,2);
		defineMedia("media.tbl");
		//String[] tileIds = new String[roxelLength];
		
//		defineImage(
//				"car", // graphic name
//				"-", 0, // tile name and tile cid (in case we use it as a tile)
//				"car_small.png", // file
//				"-" // graphical operation (may be one or two out of
//				    //"x","y", "l","r","u")
//			);
		setBGColor(JGColor.white);
		for(int i = 1; i < roxelLength+1; i++){
			for(int j = 1; j < roxelLength+1; j++){
				if(i%5 == 0 || j%5 == 0){
					setTiles(i-1, j-1,new String[] {"#"});
				}else if((i-1)%5 == 0 && (j-1)%5 == 0){
					setTiles(i-1, j-1,new String[] {"a"});
					setTiles(i-1, j,new String[] {"e"});
					setTiles(i-1, j+1,new String[] {"i"});
					setTiles(i-1, j+2,new String[] {"m"});
					setTiles(i, j-1,new String[] {"b"});
					setTiles(i, j,new String[] {"f"});
					setTiles(i, j+1,new String[] {"j"});
					setTiles(i, j+2,new String[] {"n"});
					setTiles(i+1, j-1,new String[] {"c"});
					setTiles(i+1, j,new String[] {"g"});
					setTiles(i+1, j+1,new String[] {"k"});
					setTiles(i+1, j+2,new String[] {"o"});
					setTiles(i+2, j-1,new String[] {"d"});
					setTiles(i+2, j,new String[] {"h"});
					setTiles(i+2, j+1,new String[] {"l"});
					setTiles(i+2, j+2,new String[] {"p"});
				}
			}
		}
	}

	public void doFrame() {
		// Move all objects.
		moveObjects(null,0);
		// release objects when we press the mouse button or the shift key
		// The mouse button releases a continuous stream of objects, the shift
		// key only one.
		if (getKey('C')) {	//vertical
			clearKey('C');
			boolean horizontalNotVertical = rnd.nextBoolean();
			int x;
			int y;
			if(horizontalNotVertical){
				x = (int)random(1,roxelLength*roxelWidth);
				y = (int)random(1,roxelLength/5+1)*roxelWidth*5-roxelWidth;
			}else{
				x = (int)random(1,roxelLength/5+1)*roxelWidth*5-roxelWidth;
				y = (int)random(1,roxelLength*roxelWidth);
			}
			new Car(x, y, horizontalNotVertical);
			// release a simple object in the direction in which the mouse moves
			//new Car(getMouseX()-10, getMouseY()-10, false);
		}
	}

	/** Our user-defined object. */
	class Car extends JGObject {

		double initialSpeed;
		boolean initialDirection;	//true = horizontal; false = vertical
		
		/** Constructor. */
		Car (int x, int y, boolean direction) {	//true = horizontal
			// Initialise game object by calling an appropriate constructor
			// in the JGObject class.
			super(
				"car",// name by which the object is known
				true,//true means add a unique ID number after the object name.
				     //If we don't do this, this object will replace any object
				     //with the same name.
				x,  // X position
				y, // Y position
				0, // the object's collision ID (used to determine which classes
				   // of objects should collide with each other)
				null // name of sprite or animation to use (null is none)
			);
			
			if(direction){	//horizontal
				int tempy = y;
				tempy += roxelWidth;
				tempy /= roxelWidth*5;
				
				if(tempy%2 != 0){
					xspeed = random(1.5,2.5);
					yspeed = 0;
					setGraphic("carhr");
				}else{
					xspeed = random(-2.5,-1.5);
					yspeed = 0;
					setGraphic("carhl");
				}
				initialSpeed = xspeed;
				initialDirection = true;
			}else{			//vertical
				int tempx = x;
				tempx += roxelWidth;
				tempx /= roxelWidth*5;
				
				if(tempx%2 != 0){
					xspeed = 0;
					yspeed = random(1.5,2.5);
					setGraphic("carvd");
				}else{
					xspeed = 0;
					yspeed = random(-2.5,-1.5);
					setGraphic("carvu");
				}
				initialSpeed = yspeed;
				initialDirection = false;
			}
		}

		/** Update the object. This method is called by moveObjects. */
		public void move() {
			// A very "classic" behaviour:
			// bounce off the borders of the screen.
			if (x >  pfWidth() && xspeed>0) x = -16;
			if (x <            -16 && xspeed<0) x = pfWidth()+16;
			if (y > pfHeight() && yspeed>0) y = -16;
			if (y <            -16 && yspeed<0) y = pfHeight()+16;
		}


	} /* end class Car */

}